package com.example.email.controller;

import com.example.email.entity.User;
import com.example.email.service.EnclosureService;
import com.example.email.service.MailService;
import com.example.email.service.MailexplainService;
import com.example.email.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class MainController {
    @Autowired
    MailService mailService;
    @Autowired
    EnclosureService enclosureService;
    @Autowired
    MailexplainService mailexplainService;
    @Autowired
    UserService userService;

    /**
     * 导航栏
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/index")
    public String index(Model model,HttpSession session){
        User user= (User) session.getAttribute("user");
        model.addAttribute("receivingCount",mailService.shouCount(user.getMailboxNo()));
        model.addAttribute("markCount",mailService.xingCount(user.getMailboxNo()));
        model.addAttribute("draftCount",mailService.draftCount(user.getMailboxNo()));
        model.addAttribute("deleteCount",mailService.deleteCount(user.getMailboxNo()));
        model.addAttribute("faCount",mailService.faCount(user.getMailboxNo()));
        model.addAttribute("shanCount",mailService.shanCount(user.getMailboxNo()));
        return "/index";
    }

    /**
     * 去登录页面
     * @return
     */
    @GetMapping("/login")
    public String login(){
        return "/login";
    }

    /**
     * 登录
     * @param mailboxNo
     * @param password
     * @param session
     * @return
     */
    @PostMapping("/login")
    public String login(String mailboxNo, String password, HttpSession session){
        User user=userService.logIn(mailboxNo,password);
        if (user!=null){
            session.setAttribute("user",user);
            return "redirect:/index";
        }else {
            return "/login";
        }
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @GetMapping("/exitLogin")
    public String exitLogin(HttpSession session){
        session.invalidate();
        return "/login";
    }

    /**
     * 主页面
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/indexInfo")
    public String toMailIndex(Model model,HttpSession session){
        User user= (User) session.getAttribute("user");
        model.addAttribute("receivingCount",mailService.shouCount(user.getMailboxNo()));
        model.addAttribute("markCount",mailService.xingCount(user.getMailboxNo()));
        return "/indexInfo";
    }
}
